package com.cskaoyan.demo7.aspect;

import com.cskaoyan.demo7.bean.Log;
import com.cskaoyan.demo7.service.LogService;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class LogAspect {
    @Autowired
    LogService logService;

    @Pointcut("@annotation(com.cskaoyan.demo7.annotation.Log)")
    public void pt(){}

    @AfterReturning("pt()")
    public void returningAdvice(JoinPoint joinPoint){
        String methodName = joinPoint.getSignature().getName();
        Log log = new Log();
        log.setOperationMethod(methodName);
        log.setSuccess(1);
        log.setErrorMsg("OK");
        logService.addLog(log);
    }
    @AfterThrowing(value = "pt()",throwing = "exp")
    public void expAdvice(JoinPoint joinPoint,Throwable exp){
        String methodName = joinPoint.getSignature().getName();
        Log log = new Log();
        log.setOperationMethod(methodName);
        log.setSuccess(0);
        log.setErrorMsg(exp.getMessage());
        logService.addLog(log);
    }
}
